<template>
  <div>
    <el-form :model="form" label-position="right" ref="formRef" :rules="formRules">
      <el-row :gutter="15">
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="编号" prop="number">
            <el-input v-model="form.number"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="简称" prop="forShort">
            <el-input v-model="form.forShort"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="英文名" prop="enName">
            <el-input v-model="form.enName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="负责人" prop="principal">
            <el-input v-model="form.principal"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="邮编" prop="postCode">
            <el-input v-model="form.postCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="联系人" prop="contact">
            <el-input v-model="form.contact"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="form.contactPhone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="传真" prop="fax">
            <el-input v-model="form.fax"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="组织类型" prop="companyType">
            <el-select v-model="form.companyType" clearable placeholder="请选择：">
              <el-option v-for="item in options" :key="item.number" :label="item.name" :value="item.number">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="第三方公司">
            <el-switch v-model="form.thirdCompany" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="英文地址">
            <el-input v-model="form.englishAddress"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="统一社会信用代码">
            <el-input v-model="form.unifiedSocialCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="报关企业代码">
            <el-input v-model="form.customsCompanyCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="开户行">
            <el-input v-model="form.bankName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="开户账号">
            <el-input v-model="form.bankAccount"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <ts-card title="附件">
      <tsMultiple v-model="fileList" :addTemplate="fileTemplate" @delete="deleteFile">
        <template #tableContent>
          <el-table-column label="图片" width="100">
            <template #default="scope">
              <el-image v-if="checkType(scope.row) == 'image'" alt="上传图片" :src="scope.row.fileFullPath"
                @click="addImage(scope.row.fileFullPath)" :preview-src-list="srcList" preview-teleported></el-image>
              <video v-else-if="checkType(scope.row) == 'video'" :src="scope.row.fileFullPath" controls
                style="width: 100%;height: 100%;"></video>
              <span v-else>{{ scope.row.fileName }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="fileKind" label="类别">
            <template #default="scope">
              <el-select v-model="scope.row.fileKind">
                <el-option v-for="item in configData.fileKindList" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="itemIndex" label="排序">
            <template #default="scope">
              <el-input-number v-model="scope.row.itemIndex" :min="1"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注">
            <template #default="scope">
              <el-input v-model="scope.row.remark"></el-input>
            </template>
          </el-table-column>
        </template>
      </tsMultiple>
    </ts-card>
    <tsFormCard v-model="dataList" title="银行账户" :addTemplate="bankTemplate" @delete="deleteData">
      <template #content="{ item }">
        <el-form label-width="120px" :model="item" label-position="right">
          <el-row>
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="开户行">
                <el-input v-model="item.bankName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="开户行代码">
                <el-input v-model="item.bankNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
              <el-form-item label="地址">
                <el-input v-model="item.bankAddress" type="textarea"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
              <el-form-item label="代理行名称">
                <el-input v-model="item.correspondentBankName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
              <el-form-item label="代理行代码">
                <el-input v-model="item.correspondentBankNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
              <el-form-item label="银行帐号">
                <el-input v-model="item.bankAccount"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </template>
    </tsFormCard>
    <div class="button-position">
      <el-button @click="updateVisible">取 消</el-button>
      <el-button type="primary" @click="updataCompany(formRef)">确 定</el-button>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { ElNotification } from "element-plus";
import tsUpload from "@/components/tsUpload/file.vue";
import Api from "@/api/model/user";
import tsFormCard from "@/components/tsFormCard/index.vue";
import tsMultiple from '@/components/tsUpload/multipe.vue'
import { checkType } from '@/utils/checkType.js'
const emit = defineEmits(["updatetable"]);
const props = defineProps({
  paramsData: {
    type: Object,
    defalut: {},
  },
});
onMounted(() => {
  getData();
});

// 查看用户数据的对象
const form = ref({});
const configData = ref([])
const dataList = ref([]);
const fileList = ref([])
const srcList = ref([])
const deleteList = ref([]);
const deleteFileList = ref([])
const bankTemplate = ref({});
const fileTemplate = ref({})
const formRef = ref();
const formRules = reactive({
  number: [{ required: true, message: "请输入编号", trigger: "blur" }],
  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
});
const deleteFile =(val)=>{
  deleteFileList.value.push(val)
}
const deleteData = (val) => {
  deleteList.value.push(val);
};
const addImage = (val) =>{
  srcList.value.unshift(val)
}
const updateVisible = (val) => {
  emit("updatetable");
};

const restAddFormData = () => {
  formRef.value.resetFields();
};

const options = ref([]);
const getData = async () => {
  let res = await Api.company.detail.post(props.paramsData);
  console.log(res);
  if (res.code == 200) {
    options.value = res.configData.companyTypeList;
    configData.value = res.configData
    form.value = res.data;
    dataList.value = res.dataList;
    bankTemplate.value = res.dtl;
    fileTemplate.value=res.file
    fileList.value  = res.fileList
  }
};
const updataCompany = async (formRef) => {
  await formRef.validate((valid, fields) => {
    if (valid) {
      updataCompanyData();
    }
  });
};
const updataCompanyData = async () => {
  var params = {
    bean: form.value,
    dataList: dataList.value
      .filter((ele) => {
        return ele.bankNo;
      })
      .concat(deleteList.value),
      fileList:fileList.value.concat(deleteFileList.value)
  };
  var res = await Api.company.update.post(params);
  if (res.code == 200) {
    restAddFormData();
    ElNotification.success({
      title: "",
      message: "修改成功！",
      duration: 2000,
    });
    emit("updatetable", true);
  }
};
</script>

<style lang="less" scoped></style>
